﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using LevelEditor.Objects;

namespace LevelEditor.Components
{
    public class LibraryItem : Panel
    {
        private Tile tile;
    
        public Tile Tile
        {
            get { return tile; }
        }

        private Label tileName;
        public Label TileName
        {
            get { return tileName; }
        }

        private PictureBox previewImage;
        public PictureBox PreviewImage
        {
            get { return previewImage; }
        }

        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }

        }

        private Color selectedColor;
        public Color SelectedColor
        {
            get { return selectedColor; }
            set { selectedColor = value; }
        }

        PictureBox backgroundImage;

        private Color defaultColor;

        private Font font;

        private LevelEditor frm;

        private List<Tile> instances;

        public LibraryItem(Tile tile, LevelEditor frm) : base()
        {
            this.font = new Font(FontFamily.GenericSansSerif, 10);
            this.tile = tile;
            this.tile.Father = this;
            this.instances = new List<Tile>();

            this.previewImage = new PictureBox();
            previewImage.Image = new Bitmap(tile.Image, new Size(34, 36));
            previewImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            previewImage.Width = previewImage.Image.Width;
            previewImage.Height = previewImage.Image.Height;
            this.previewImage.Enabled = false;

            this.tileName = new Label();
            this.tileName.Width = 50;
            this.tileName.Font = this.font;
            this.TileName.Text = this.tile.Name;
            this.tileName.AutoSize = false;
            this.tileName.TextAlign = ContentAlignment.MiddleCenter;
            this.tileName.BackColor = Color.Transparent;
            this.tileName.Dock = DockStyle.Fill;
            this.tileName.Padding = new Padding(10, 0, 0, 0);
            this.tileName.Enabled = false;

            this.defaultColor = Color.LightGray;
            this.selectedColor = Color.LightSkyBlue;
            this.BackColor = defaultColor;
            this.Width = 110;
            this.Height = 36;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.frm = frm;
            this.Padding = new Padding(0);
            this.Margin = new Padding(2,0,0,0);

            backgroundImage = new PictureBox
            {
                Dock = DockStyle.Left,
                SizeMode = PictureBoxSizeMode.StretchImage,
                Height = this.Height,
                Width = this.Width,
            };
            backgroundImage.Image = new Bitmap(@"Images\tiles_chars_items_rooms_button.png");
            backgroundImage.Enabled = false;

            
            this.Controls.Add(previewImage);
            backgroundImage.Controls.Add(tileName);
            this.Controls.Add(backgroundImage);

            MenuItem mItem = new MenuItem("Create Behavior");
            mItem.Click += mItem_Click;

            this.ContextMenu = new ContextMenu(new MenuItem[] { mItem });

        }

        void mItem_Click(object sender, EventArgs e)
        {
            // call bd to create a new behavior
            NewBehavior newBehavior = new NewBehavior();
            newBehavior.IsSelectingBehavior = true;
            if (newBehavior.ShowDialog() == DialogResult.OK)
            {
                this.Tile.standAloneBehaviors.Add(newBehavior.CreatedBehavior);
            }
        }

        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.Delete();
            }
            base.OnPreviewKeyDown(e);
        }

        public void Deselect()
        {
            isSelected = false;
            this.BackColor = defaultColor;
        }

        public new void Select()
        {
            isSelected = true;
            this.BackColor = selectedColor;

            this.Focus();
        }

        public void AddIntance(Tile tile)
        {
            instances.Add(tile);
        }

        public void RemoveInstance(Tile tile)
        {
            instances.Remove(tile);
        }

        public void Delete()
        {
            for (int k = 0; k < instances.Count; k++)
            {
                this.frm.Controls.Remove(instances[k]);
            }

            this.Parent.Controls.Remove(this);
        }

    }
}
